package com.caption.convert;
import com.caption.dto.CardDTO;
import com.caption.dto.SchoolDTO;
import com.caption.dto.UserDTO;
import com.caption.vo.CardVO;
import com.caption.vo.SchoolVO;
import com.caption.vo.UserVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 注意实体类不能以只有一个字母开头的驼峰命名
 * 例如 cName,CName等不被识别
 * 正确 caName
 */
@Mapper
public abstract class UserConvert {
    public static UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);

    /**
     * dto转vo
     * 1.BigDecimal会默认转string
     * 2.dateFormat 指定日期转换string格式
     * 3.numberFormat 只保留两位小数
     * @param userDTO
     * @return
     */
    @Mappings( {
            @Mapping(source = "height", target = "height", numberFormat = "#.00"),
            @Mapping(source = "createTime", target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss"),
            @Mapping(source = "firstDate", target = "firstDate", qualifiedByName = "convertDate"),
            @Mapping(source = "schoolDTO", target = "schoolVO"),
            @Mapping(source = "cardDTOList", target = "cardVOList")
    })
    public abstract UserVO dto2vo(UserDTO userDTO);

    /**
     * list 转换
     * @param cardDTOList
     * @return
     */
    @Mappings( {
            @Mapping(source = "height", target = "height", numberFormat = "#.00"),
            @Mapping(source = "createTime", target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss"),
            @Mapping(source = "firstDate", target = "firstDate", qualifiedByName = "convertDate"),
            @Mapping(source = "schoolDTO", target = "schoolVO"),
            @Mapping(source = "cardDTOList", target = "cardVOList")
    })
    public abstract List<UserVO> dtoList2Vo(List <UserDTO> cardDTOList);



    /**
     * 对象内实体类转换, 会自动调用
     * @param schoolDTO
     * @return
     */
    @Mappings( {
            @Mapping(source = "dept", target = "scDept"),
            @Mapping(source = "name", target = "scName")
    })
     abstract SchoolVO schoolDto2Vo(SchoolDTO schoolDTO);

    /**
     * 对象内 list<dto>转 list<vo>
     * @param cardDTO
     * @return
     */
    @Mappings( {
            @Mapping(source = "id", target = "cid"),
            @Mapping(source = "name", target = "cname")
    })
     abstract CardVO cardDto2VO(CardDTO cardDTO);







    /**
     * 自定义方法
     * @param dateStr
     * @return
     */
    @Named("convertDate")
     Date convertDate(String dateStr) {
        if(dateStr==null){
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}
